package org.nightrunner.d3.controller.management;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.nightrunner.d3.constant.CRUDConstant;
import org.nightrunner.d3.entity.Prefix;
import org.nightrunner.d3.log.SysLog;
import org.nightrunner.d3.model.DefaultPaginationInfo;
import org.nightrunner.d3.model.IPaginationInfo;
import org.nightrunner.d3.model.PaginationModel;
import org.nightrunner.d3.service.IPrefixService;
import org.nightrunner.d3.service.ISearchService;
import org.nightrunner.d3.util.OperateInfo;
import org.nightrunner.d3.util.OperateInfo.Link;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.JsonObject;

/**
 * Prefix Management Controller
 * 
 * @author haiyang.chen
 * @date 2012-5-15
 * @version 1.0
 */
@Controller
@RequestMapping("/management/equipment/prefix/")
public class PrefixManagementController implements CRUDConstant {

	private static final String RETURN_VIEW_PREFIX = "/management/equipment/prefix/";

	private static final String LINK_INDEX = "/management/equipment/prefix/index";

	private static final String LINK_NAME_INDEX = "属性管理首页";

	public static final Logger LOGGER = SysLog.CONTROLLER;

	@Autowired
	private IPrefixService prefixService;

	public IPrefixService getPrefixService() {
		return prefixService;
	}

	public void setPrefixService(IPrefixService prefixService) {
		this.prefixService = prefixService;
	}

	@RequestMapping(value = "list-{currentPage}-{rowSize}", method = RequestMethod.GET)
	public String list(HttpServletResponse response,
			HttpServletRequest request, ModelMap modelMap,
			@PathVariable("rowSize") Integer rowSize,
			@PathVariable("currentPage") Integer currentPage) {
		IPaginationInfo pagination = new DefaultPaginationInfo();
		pagination.setCurrentPage(currentPage);
		pagination.setRowSize(rowSize);

		PaginationModel<List<Prefix>> paginationModel = prefixService
				.getPrefixs(pagination);
		modelMap.addAttribute("paginationModel", paginationModel);
		return RETURN_VIEW_PREFIX + VIEW_LIST;
	}

	@RequestMapping(value = "index", method = RequestMethod.GET)
	public String index(HttpServletResponse response,
			HttpServletRequest request, ModelMap modelMap) {
		return list(response, request, modelMap, 10, 0);
	}

	@RequestMapping(value = "", method = RequestMethod.GET)
	public String root(HttpServletResponse response,
			HttpServletRequest request, ModelMap modelMap) {
		return list(response, request, modelMap, 10, 0);
	}

	@RequestMapping(value = "preupdate/{prefixId}", method = RequestMethod.GET)
	public String preUpdate(HttpServletResponse response,
			HttpServletRequest request, ModelMap modelMap,
			@PathVariable("prefixId") String prefixId) {
		Prefix prefix = prefixService.getPrefix(prefixId);
		modelMap.addAttribute("prefix", prefix);
		return RETURN_VIEW_PREFIX + VIEW_UPDATE;
	}

	@RequestMapping(value = "add", method = RequestMethod.POST)
	public String add(HttpServletResponse response, HttpServletRequest request,
			ModelMap modelMap) {

		Prefix prefix = new Prefix();
		String prefixName = request.getParameter("prefixName");
		String prefixLevel = request.getParameter("prefixLevel");
		String prefixKeywords = request.getParameter("prefixKeywords");
		String prefixDescription = request.getParameter("prefixDescription");

		prefix.setPrefixName(prefixName);
		prefix.setPrefixLevel(Integer.parseInt(prefixLevel));
		prefix.setPrefixDescription(prefixDescription);
		prefix.setPrefixKeywords(prefixKeywords);

		prefixService.updatePrefix(prefix);

		OperateInfo operateInfo = new OperateInfo();
		operateInfo.setInfo(OPERATE_INFO_UPDATE_OK);
		operateInfo.addLink(new Link(LINK_NAME_INDEX, LINK_INDEX));
		modelMap.addAttribute(REQUEST_KEY_OPERATE_INFO, operateInfo);

		return VIEW_OPERATE_INFO;
	}

	@RequestMapping(value = "preadd", method = RequestMethod.GET)
	public String preAdd(HttpServletResponse response,
			HttpServletRequest request, ModelMap modelMap) {

		// Prefix prefix = prefixService.getPrefix(prefixId);
		//
		// String prefixName = request.getParameter("prefixName");
		// String prefixKeywords = request.getParameter("prefixKeywords");
		// String prefixItemLevel =
		// request.getParameter("prefixItemLevel");
		// String prefixDescription = request
		// .getParameter("prefixDescription");
		//
		// prefix.setPrefixName(prefixName);
		// prefix.setPrefixDescription(prefixDescription);
		// prefix.setPrefixItemLevel(prefixItemLevel);
		// prefix.setPrefixKeywords(prefixKeywords);
		//
		// prefixService.updatePrefix(prefix);
		//
		// OperateInfo operateInfo = new OperateInfo();
		// operateInfo.setInfo(OPERATE_INFO_UPDATE_OK);
		// operateInfo.addLink(new Link(LINK_NAME_INDEX, LINK_INDEX));
		// modelMap.addAttribute(REQUEST_KEY_OPERATE_INFO, operateInfo);

		return RETURN_VIEW_PREFIX + VIEW_ADD;
	}

	@RequestMapping(value = "update/{prefixId}", method = RequestMethod.POST)
	public String update(HttpServletResponse response,
			HttpServletRequest request, ModelMap modelMap,
			@PathVariable("prefixId") String prefixId) {

		Prefix prefix = prefixService.getPrefix(prefixId);

		String prefixName = request.getParameter("prefixName");
		String prefixKeywords = request.getParameter("prefixKeywords");
		String prefixLevel = request.getParameter("prefixLevel");
		String prefixDescription = request.getParameter("prefixDescription");

		prefix.setPrefixName(prefixName);
		prefix.setPrefixLevel(Integer.parseInt(prefixLevel));
		prefix.setPrefixDescription(prefixDescription);
		prefix.setPrefixKeywords(prefixKeywords);

		prefixService.updatePrefix(prefix);

		OperateInfo operateInfo = new OperateInfo();
		operateInfo.setInfo(OPERATE_INFO_UPDATE_OK);
		operateInfo.addLink(new Link(LINK_NAME_INDEX, LINK_INDEX));
		modelMap.addAttribute(REQUEST_KEY_OPERATE_INFO, operateInfo);

		return VIEW_OPERATE_INFO;
	}

	@Autowired
	private ISearchService searchService;

	public ISearchService getSearchService() {
		return searchService;
	}

	public void setSearchService(ISearchService searchService) {
		this.searchService = searchService;
	}

	// @RequestMapping(value = "search/{keyword}-{currentPage}-{rowSize}",
	// method = RequestMethod.GET)
	// public String result(ModelMap modelMap,
	// @PathVariable("keyword") String keyword,
	// @PathVariable("currentPage") Integer currentPage,
	// @PathVariable("rowSize") Integer rowSize) throws Exception {
	// if (keyword == null || keyword.trim().length() == 0) {
	// return "redirect:/" + RETURN_VIEW_PREFIX + VIEW_INDEX;
	// }
	//
	// IPaginationInfo pagination = new DefaultPaginationInfo();
	// pagination.setCurrentPage(currentPage);
	// pagination.setRowSize(rowSize);
	//
	// List<String> keywords = new ArrayList<String>();
	// keywords.add(keyword);
	//
	// PaginationModel<List<Prefix>> paginationModel = searchService
	// .searchPrefixs(keywords, pagination);
	// modelMap.addAttribute("keyword", keyword);
	//
	// modelMap.addAttribute("paginationModel", paginationModel);
	//
	// return RETURN_VIEW_PREFIX + "search/" + VIEW_LIST;
	// }

	@RequestMapping(value = "delete/{prefixId}", method = { RequestMethod.GET,
			RequestMethod.POST })
	@ResponseBody
	public String delete(@PathVariable("prefixId") String prefixId,
			HttpServletResponse response, ModelMap modelMap) {
		JsonObject object = new JsonObject();
		Prefix prefix = prefixService.getPrefix(prefixId);

		if (prefix == null) {
			object.addProperty(CRUD_SUCCESS, false);
			object.addProperty(CRUD_INFO, "事项不存在");
		} else {
			try {
				prefixService.deletePrefix(prefix);
				object.addProperty(CRUD_SUCCESS, true);
			} catch (Exception ex) {
				object.addProperty(CRUD_SUCCESS, false);
			}
		}

		return object.toString();
	}
}
